var e = require("../../../utils/formate.js"), t = (require("../../../utils/moment.js"),
  require("../../../utils/util.js"), getApp()), a = getApp().getAccessToken, s = getApp().getAccStatusText;
var session = require('../../../utils/session.js');
let app = getApp()

import mydata from './data.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:[],
    hidden: true,
    nocancel: false,
    imei: "",
    startX: 0,
    startY: 0,
    modalHidden: !0,
    isHiddenEmptyContent: !0,
    image_prefix: t.system.image_prefix,
    isShowConfirm: !1,
    isAllowAlarmSetting: !!wx.getStorageSync("isAllowAlarmSetting"),

    // height: 0,
    scrollY: true,
    // windowHeight: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // var windowHeight = wx.getSystemInfoSync().windowHeight
    // var height = windowHeight
    that.setData({
      // array: mydata.listDatas,
      // height:height,
      // windowHeight: windowHeight
    })
    options.imei && this.bindImeiToUser(options.imei);
    console.log(this.data.array)
  },

  cancel: function () {
    this.setData({
      hidden: true
    });
  },
  confirm: function () {
    var that = this
    this.setData({
      hidden: true
    });
    let imei = that.data.imei.trim();
    this.bindImeiToUser(imei)
  },

  setImei: function (e) {
    this.setData({
      imei: e.detail.value
    });
  },

  toggleDialog: function() {
    this.setData({
      hidden: false
    });
  },

  gotoscan: function () {
    wx.scanCode({
      success(res) {
        t.result && e.setData({
          imei: t.result
        });
      }
    })
  },
  setImei: function (e) {
    this.setData({
      imei: e.detail.value
    });
  },
  /**
  * 处理touchstart事件
  */
  handleTouchStart(e) {
    this.startX = e.touches[0].pageX
  },

  /**
   * 处理touchend事件
   */
  handleTouchEnd(e) {
    if (e.changedTouches[0].pageX < this.startX && e.changedTouches[0].pageX - this.startX <= -30) {
      this.showDeleteButton(e)
    } else if (e.changedTouches[0].pageX > this.startX && e.changedTouches[0].pageX - this.startX < 30) {
      this.showDeleteButton(e)
    } else {
      this.hideDeleteButton(e)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCarList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getCarList: function() {
    let that = this
    let openId = wx.getStorageSync("wechatOpenid")
    wx.request({
      url: app.system.app_url_prefix + "/weapp/userinfo/bind/query?openId=" + openId,
      method: "POST",
      data: {"openId": openId},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (resp) {
        let code = parseInt(resp.data.code)
        if (code == 0) {
          let data = resp.data.list
          console.log(data)
          that.setData({
            array: data,
          })
          wx.setStorageSync("devsum", Object.keys(data).length)
        } else wx.showModal({
          title: "提示",
          content: "获取设备列表出错",
          showCancel: !1,
          success: function (e) { }
        });
      },
      fail: function (e) {
        wx.showModal({
          title: "提示",
          content: "获取设备列表出错",
          showCancel: !1,
          success: function (e) { }
        });
      }
    })
  },

  bindImeiToUser: function (t) {
    let openId = wx.getStorageSync("wechatOpenid")
    wx.showLoading({})
    wx.request({
      url: app.system.app_url_prefix + "/weapp/userinfo/bind?openId=" + openId + "&code=" + t,
      method: "POST",
      data: {},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        200 === e.statusCode ? e.data && 0 === parseInt(e.data.code) ? wx.reLaunch({
          url: "../../home/index/index?imei=" + t
        }) : wx.showModal({
          title: "提示",
          content: e.data.result,
          showCancel: !1,
          success: function (e) { }
        }) : wx.showModal({
          title: "提示",
          content: "添加设备失败",
          showCancel: !1,
          success: function (e) { }
        });
      },
      fail: function (e) { },
      complete: function (e) {
        wx.hideLoading();
      }
    });
  },


  _del: function (e) {
    let id = e.currentTarget.id
    var s = this
    var i = parseInt(id)
    let openId = wx.getStorageSync("wechatOpenid")
    wx.request({
      url: t.system.app_url_prefix + "/weapp/userinfo/delDev?id=" + id + "&openId=" + openId,
      data: {},
      method: "DELETE",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (t) {
        if(200 == t.statusCode && "0" === t.data.code) {
          s.handleDeleteProduct(e)
        }
         
      },
      fail: function (e) {
        wx.showModal({
          title: "提示",
          content: "删除设备出错",
          showCancel: !1,
          success: function (e) { }
        });
      },
      complete: function (e) {
        wx.hideLoading();
      }
    });
  },
  del: function (e) {
    wx.showLoading({}), this._del(e);
  },

  switch: function (e) {
    var t = e.currentTarget.dataset.id;
    // t.isUnused ? wx.showModal({
    //   title: "提示",
    //   content: "未启用设备",
    //   showCancel: !1,
    //   success: function (e) { }
    // }) : t.isNeedPay ? wx.showModal({
    //   title: "提示",
    //   content: "设备已过期，请联系客服续费",
    //   showCancel: !1,
    //   success: function (e) { }
    // }) : wx.reLaunch({
    //   url: "../index/index?imei=" + t.imei
    // });
   
    wx.reLaunch({
      url: "../../home/index/index?vehicleId=" + this.data.array[t].vehicleId
    })
  },

  /**
   * 显示删除按钮
   */
  showDeleteButton: function (e) {
    let productIndex = e.currentTarget.dataset.id
    this.setXmove(productIndex, -65)
  },

  /**
   * 隐藏删除按钮
   */
  hideDeleteButton: function (e) {
    let productIndex = e.currentTarget.dataset.id

    this.setXmove(productIndex, 0)
  },

  /**
   * 设置movable-view位移
   */
  setXmove: function (productIndex, xmove) {
    let array = this.data.array
    array[productIndex].xmove = xmove

    this.setData({
      array: array
    })
  },

  /**
   * 处理movable-view移动事件
   */
  handleMovableChange: function (e) {
    if (e.detail.source === 'friction') {
      if (e.detail.x < -30) {
        this.showDeleteButton(e)
      } else {
        this.hideDeleteButton(e)
      }
    } else if (e.detail.source === 'out-of-bounds' && e.detail.x === 0) {
      this.hideDeleteButton(e)
    }
  },
  /**
  * 删除产品
  */
  handleDeleteProduct: function (e) {
    let array = this.data.array
    let productIndex = parseInt(e.currentTarget.id)

    array.splice(productIndex, 1)

    this.setData({
      array
    })
    if (array[productIndex]) {
      this.setXmove(productIndex, 0)
    }
  },
})